package it.fuesi.expired.fragments;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.widget.DatePicker;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import it.fuesi.expired.R;
import it.fuesi.expired.formbinders.AbstractFormBinder;

/**
 * Created by pascotto on 16/09/14.
 */
public class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {

    private AbstractFormBinder listener;
    private Date currentDate = new Date();

    public void setListener(AbstractFormBinder listener){
        this.listener = listener;
    }

    public void setCurrentDate(Date currentDate){
        this.currentDate = currentDate;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        final SimpleDateFormat dfDay = new SimpleDateFormat("dd");
        final SimpleDateFormat dfMonth = new SimpleDateFormat("MM");
        final SimpleDateFormat dfYear = new SimpleDateFormat("yyyy");

        int day = Integer.parseInt(dfDay.format(currentDate));
        int month = Integer.parseInt(dfMonth.format(currentDate))-1;
        int year = Integer.parseInt(dfYear.format(currentDate));

        //FIXME


        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(year, month, day);
        SimpleDateFormat sdf = new SimpleDateFormat(getActivity().getString(R.string.date_format));
        String formattedDate = sdf.format(c.getTime());

        if (listener != null)
        {
            listener.returnDate(formattedDate);

        }

    }
}